/**
 * Panel permettant de choisir les destinataires
 * 
 * @author Marie-Hélène Goddet, Damien Finck, Cédric Schaller
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import utils.Resources;
import fastwriting.Contact;

@SuppressWarnings("serial")
public class JContact extends JPanel {
	
	private ActionListener actionListener;
	
	protected JPanel jp_haut;
		protected JButton jb_home;
		protected JLabel jl_contact;
		protected Component horizontalStrut;
	protected JList jlst_contact;
	protected JScrollPane jsp_contact;
	protected JPanel jp_centre;
		protected JPanel jp_centre_haut;
			protected JLabel jl_a;
			protected JButton jb_toRight;
			protected JButton jb_toLeft;
	protected JList jlst_to;protected JScrollPane jsp_to;
	protected JPanel jp_bottom;
		protected JLabel jl_nbContact;
		protected JButton jb_next;
	
	protected DefaultListModel lm_contact,lm_to;
	
	/**
	 * Constructeur
	 */
	public JContact() {
		this.lm_contact = new DefaultListModel();
		this.lm_to = new DefaultListModel();
		Vector<Contact> c = Contact.lireContact();
		for (int i=0;i<c.size();i++) {
			this.lm_contact.addElement(c.get(i));
			//Debug.addlog(c.get(i).toString());
		}
		/** Evénements **/
		ActionListener action = new Event_JPContact(this);
		ListSelectionListener list = new Event_JPContact(this);
		/** Création des éléments     **/
		this.jp_haut = new JPanel(new BorderLayout());
			this.jb_home = new JButton("Accueil",Resources.getImageIcon("images/home.png",getClass()));
			this.jl_contact = new JLabel("Contacts",SwingConstants.CENTER);
			this.horizontalStrut = Box.createHorizontalStrut(101);
		this.jlst_contact = new JList(this.lm_contact);
		this.jsp_contact = new JScrollPane(this.jlst_contact);
		this.jp_centre = new JPanel();
			this.jp_centre_haut = new JPanel(new GridLayout(3,1));
			this.jl_a = new JLabel("À :");
			this.jb_toRight = new JButton("->");
			this.jb_toLeft = new JButton("<-");
		this.jlst_to = new JList(this.lm_to);
		this.jsp_to = new JScrollPane(this.jlst_to);
		this.jp_bottom = new JPanel(new GridLayout());
			this.jl_nbContact = new JLabel("Nombre de contacts : "+this.lm_contact.getSize());
			this.jb_next = new JButton("suivant");
		/** Paramétrage des éléments  **/
		Font f2 = new Font("Default",Font.BOLD,22);
		this.jl_contact.setFont(f2);
		this.jsp_contact.setPreferredSize(new Dimension(300,1));
		this.jsp_to.setPreferredSize(new Dimension(300,1));
		//this.jb_home.setPreferredSize(new Dimension(100,30));
		this.setLayout(new BorderLayout());
		
		this.jsp_contact.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.jsp_contact.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.jsp_to.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.jsp_to.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		this.jb_toRight.setEnabled(false);
		this.jb_toLeft.setEnabled(false);
		
		this.jp_centre_haut.setPreferredSize(new Dimension(70,100));
		//this.jb_toRight.setPreferredSize(new Dimension(70,10));
		//this.jb_toLeft.setPreferredSize(new Dimension());
		/** Action sur les éléments   **/
		this.jb_home.addActionListener(action);
		this.jb_next.addActionListener(action);
		
		this.jb_toRight.addActionListener(action);
		this.jb_toLeft.addActionListener(action);
		
		this.jlst_contact.addListSelectionListener(list);
		this.jlst_to.addListSelectionListener(list);
		
		/** Montage des éléments      **/
		this.add(this.jp_haut,BorderLayout.NORTH);
			this.jp_haut.add(this.jb_home,BorderLayout.WEST);
			this.jp_haut.add(this.jl_contact,BorderLayout.CENTER);
			this.jp_haut.add(this.horizontalStrut,BorderLayout.EAST);
		this.add(this.jsp_contact,BorderLayout.WEST);
		this.add(this.jp_centre,BorderLayout.CENTER);
			this.jp_centre.add(this.jp_centre_haut);
				this.jp_centre_haut.add(this.jl_a);
				this.jp_centre_haut.add(this.jb_toRight);
				this.jp_centre_haut.add(this.jb_toLeft);
		this.add(this.jsp_to,BorderLayout.EAST);
		this.add(this.jp_bottom,BorderLayout.SOUTH);
			this.jp_bottom.add(this.jl_nbContact);
			this.jp_bottom.add(this.jb_next);
		
	}
	public void addActionListener(ActionListener actionListener) {
		this.actionListener = actionListener;
	}
	public ActionListener getActionListener() {
		return actionListener;
	}
}
class Event_JPContact implements ActionListener, ListSelectionListener {
	JContact jp;
	/**
	 * Constructeur
	 * @param jp le panel de contact
	 */
	public Event_JPContact(JContact jp) {
		this.jp=jp;
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object obj = arg0.getSource();
		if (obj instanceof JButton) {
			JButton jb = (JButton)obj;
			if (jb==jp.jb_next) {
				ActionEvent e = new ActionEvent(jp,1001,"JPContact_next");
				jp.getActionListener().actionPerformed(e);
			} else if (jb==jp.jb_home) {
				ActionEvent e = new ActionEvent(jp,1001,"JPContact_home");
				jp.getActionListener().actionPerformed(e);
			} else if (jb==jp.jb_toRight) {
				while(jp.jlst_contact.getSelectedIndex()!=-1) {
					jp.lm_to.addElement(jp.jlst_contact.getSelectedValue());
					jp.lm_contact.remove(jp.jlst_contact.getSelectedIndex());
				}
			} else if (jb==jp.jb_toLeft) {
				while (jp.jlst_to.getSelectedIndex()!=-1) {
					jp.lm_contact.addElement(jp.jlst_to.getSelectedValue());
					jp.lm_to.remove(jp.jlst_to.getSelectedIndex());
				}
			}
		}
	}
	/**
	 * Appelé lors de la modification des destinataires
	 */
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		Object obj = arg0.getSource();
		if(obj instanceof JList) {
			JList jlst_l = (JList)obj; 
			if(jlst_l==jp.jlst_to) {
				if(jlst_l.isSelectionEmpty())
					jp.jb_toLeft.setEnabled(false);
				else
					jp.jb_toLeft.setEnabled(true);
			}
			else if(jlst_l==jp.jlst_contact) {
				if(jlst_l.isSelectionEmpty())
					jp.jb_toRight.setEnabled(false);
				else
					jp.jb_toRight.setEnabled(true);
			}
		}
	}
}